import {  constantRouterMap } from '@/router'
import {  getRoleType } from '@/utils/auth'
import Layout from '@/views/layout/Layout'
const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: [],
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    GenerateRoutes({ commit, rootState }) {
      return new Promise(resolve => {
        let accessedRouters = [];
        	let menuList = rootState.user.menu;
        	let homeArray = []
        	menuList.forEach(function(item,index,arry){
        		item.component = Layout;
        		if(item.children&&item.children.length>=1){
        			item.children.forEach(function(chItem, chIndex,chArry){
        				let path = chArry[0].path;
        				let comName = chItem.component;
        				if(typeof chItem.component=='string'){
										chItem.component = () => import(`@/views` + comName )
        				};
        				item["redirect"] = item.path+"/"+path;
        				item["meta"] = { "title": item.title,"icon": item.icon};
        				chItem["meta"] = { "title": chItem.title}
        			});
        		};
        		
        	})
        	accessedRouters = menuList;
					//根据角色类型控制首页显示
					let roleTypeObject = {
						2: () => import('@/views/dashboard/company'),
						3: () => import('@/views/dashboard/department'),
						1: () => import('@/views/dashboard/qualityin'),
					};
					let roleType = getRoleType();
					accessedRouters.push(
						{
						path: '*',
						redirect: '/404',
						hidden: true
						},
						{
							path: '/',
							component: Layout,
							redirect: '/dashboard',
							name: 'dashboard',
							hidden: true,
							children: [{
									path: 'dashboard',
									component: roleTypeObject[roleType]
								},
							]
						}
						
					);	
        commit('SET_ROUTERS', accessedRouters)
        resolve()
      })
    }
  }
}

export default permission
